var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    allPrice:0,//商品总额
    payPrice:0,//实付金额
    isReady:true,
    remark:'',
    submitting:false,
    singleCount:0,
    singlePrice:0,
    count:0,
    order_id:0,
    order:{},
    payMoney:0,
    freeMoney:0,
    is_reg:0,
    address_id:0,
    orderMonths:0,
    beginDate:'请选择交费开始时间',
    endDate:'请选择交费结束时间',
    address:{},
    estate_id:0,
    estate_name:'',
    currentDate:new Date(2022, 0, 1),
    minDate: new Date(2022, 0, 1).getTime(),
    dateType:0,
    isShowPopup:false,
    real_name:'',
    phone_num:'',
    roomno:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options',options)
    var that = this
    this.data.order_id = parseInt(options.order_id);
    if(options.address_id){
      this.data.address_id = parseInt(options.address_id);
      this.onHttpAddressDetail()
    }
    if(options.is_reg){
      this.data.is_reg = parseInt(options.is_reg);
      this.setData({
        is_reg: parseInt(options.is_reg)
      })
    }
    console.log('this.data.is_reg ',this.data.is_reg )
    if(this.data.is_reg == 1){
      wx.setNavigationBarTitle({
        title: '物业费交费申请',
      })
    }
    if(this.data.is_reg == 0){
      that.onHttpInit()
    } 
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (t) {
    console.log('onShow estate_id',this.data.estate_id)
    console.log('onShow estate_name',this.data.estate_name)
    this.setData({
      estate_name:this.data.estate_name
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/property/order/detail', { 
      order_id:that.data.order_id
    }, 
      function (res) {
        that.setData({ isReady: true, isShouquan: 'yes' })
        console.log('获取物业费订单详情', res)
        if (res.data.code == 0) {
          that.data.order = res.data.data.order;
          that.setData({
            order: that.data.order,
            orderMonths: that.data.order.months!=undefined?that.data.order.months: 0,
            beginDate:that.data.order.beginDate!=undefined?that.data.order.endDate:'请选择日期',
            endDate:that.data.order.endDate!=undefined?that.data.order.endDate:'请选择日期',
            payMoney: that.data.order.payMoneyFen,
            freeMoney:that.data.order.freeMoney,
          })
          wx.stopPullDownRefresh() //停止下拉刷新
        } else {
          console.log("获取物业费订单详情 异常", res.data.msg)
        }
    },function(){})
  },
  onClickEstate() {
    console.log('onClickEstate')
    wx.navigateTo({
      url: '/pages/estate/list/index',
    })
  },
  bindInput(e){
    console.log('bindInput',e)
    var key = e.currentTarget.dataset.key;
    if(key == "name"){
      this.data.real_name = e.detail;
    } else if(key == "phone"){
      this.data.phone_num = e.detail;
    } else if(key == "roomno"){
      this.data.roomno = e.detail;
    }
  },
  requestPay:function(){
    var that = this
    fetchApi.httpPost('/pay/requestPayProperty', {
      order_id: that.data.order_id,
      subject:'交物业费'
    }, function (res) {
      console.log('提交订单',res)
      if (res.data.code == 0) {
        var pay_data = res.data.data;
        var wxpayinfo = JSON.parse(res.data.data)
        console.log('pay_data',pay_data)
        if(wxpayinfo.result_code == 'SUCCESS'){
          wx.requestPayment({
            timeStamp: wxpayinfo.timeStamp,
            nonceStr: wxpayinfo.nonceStr,
            package: wxpayinfo.package,
            signType: wxpayinfo.signType,
            paySign: wxpayinfo.paySign,
            success (res) { 
              console.log('requestPayment success',res)
              wx.redirectTo({
                url: '/pages/pay/payResult/index?type=wyfee&order_id=' + that.data.order_id,
              })
            },
            fail (res) {
              console.log('requestPayment fail',res)
             }
          })
        }else if(wxpayinfo.result_code == 'NOT_NEED_PAY') {//不跳转支付
          wx.redirectTo({
            url: '/pages/pay/payResult/index?type=wyfee&order_id=' + that.data.order_id,
          })
        }
      } else {
        if(res.data.msg.indexOf("超过商户单笔限额上限") >= 0){
          Toast("超过限额上限,请稍后再试");
        }else{
          Toast(res.data.msg);
        }
        that.setData({ isLoading: false, loading: true, netWork: true })
      }
    }, function () {
        that.setData({  isLoading: false,  loading: true,  netWork: true, }) 
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpAddressDetail: function () {
    var that = this;
    fetchApi.httpPost('/user/address/detail', { 
      address_id:that.data.address_id,
    }, function (res) {
        console.log('获取地址详情', res)
        if (res.data.code == 0) {
          that.setData({
            address: res.data.data.address,
            estate_name:res.data.data.estate_name,
          })
        } else {
          Toast.fail(res.data.msg);
          console.log("获取地址详情 异常", res.data.msg)
        }
    },function(){});
  },
  submitPropertyOrderReg(){
    if(this.data.estate_name == ''){
      Toast.fail("请选择所属小区");
      return;
    }

    if(this.data.roomno == ''){
      Toast.fail("请输入房屋户号");
      return;
    }
    this.onHttpAddPropertyOrder();
  },
  onHttpAddPropertyOrder(){
    var that = this;
    fetchApi.httpPost("/property/addOrderByAddress", {
      estate_id: that.data.estate_id,
      address_id: that.data.address_id,
      estate_name:that.data.estate_name,
      real_name:that.data.real_name,
      phone_num:that.data.phone_num,
      room_no:that.data.roomno,
    }, function (res) {
      var code = res.data.code;
      if (code == 0) {
        Toast.success(res.data.msg); 
        wx.redirectTo({
          url: '/pagesProperty/pages/propertyList/index',
        })
      } else {
        Toast.fail(res.data.msg); 
        return;
      }
    },function(){ })
  },
  handleDate(e){
    console.log('handleDate',e)
    if(e!=undefined){
      var type = e.currentTarget.dataset.type;
      if(type !=undefined){
        this.data.dateType = parseInt(type);
      }
    }
    console.log('handleDate this.data.dateType',this.data.dateType)
    this.setData({
      isShowPopup:!this.data.isShowPopup
    })
  },
  onConfirmDate(e){
    console.log('onConfirmDate',e)
    var time = new Date(e.detail);
    var month = time.getMonth() + 1;
    var date = time.getFullYear() + '-' + (month < 10 ? '0' + month : month );
    console.log('date',date)

    console.log('this.data.dateType',this.data.dateType)

    if(this.data.dateType == 0){
      this.data.beginDate = date;
      this.setData({
        beginDate: date
      })
    } else if(this.data.dateType == 1){
      this.data.endDate = date;
      this.setData({
        endDate: date
      })
    }
    this.setData({
      isShowPopup:!this.data.isShowPopup
    })
  },
  onCancelDate(e){
    console.log('onCancelDate',e)
    this.handleDate()
  },
})